package com.dave.gd.core.base.service.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.dave.gd.common.log.ILogger;
import com.dave.gd.common.log.LoggerFactory;
import com.dave.gd.core.base.entity.impl.AbstractBaseEntity;
import com.dave.gd.core.base.repository.IBaseRepository;
import com.dave.gd.core.base.service.IGenericService;
import com.dave.gd.core.common.MybatisTemplate;

@SuppressWarnings("rawtypes")
public abstract class AbstractGenericService<T extends AbstractBaseEntity, ID extends Serializable>
    implements IGenericService<T, ID> {
    protected ILogger logger = LoggerFactory.getLogger(this.getClass());
    @SuppressWarnings("unused")
    private final Class<T> entityClass;
    
    @SuppressWarnings("unchecked")
    public AbstractGenericService() {
        Class<?> clazz = getClass();
        if (getClass().getSimpleName().indexOf("$$EnhancerByCGLIB$$") != -1) {
            clazz = clazz.getSuperclass();
        }
        this.entityClass = (Class<T>) ((ParameterizedType) clazz.getGenericSuperclass())
            .getActualTypeArguments()[0];
    }
    
    @Autowired
    private MybatisTemplate mybatisTemplate;
    
    @Autowired
    @Qualifier("defaultRepository")
    private IBaseRepository defaultRepository;
    
    public IBaseRepository getDefaultRepository() {
        return defaultRepository;
    }
    
    public MybatisTemplate getMybatisTemplate() {
        return mybatisTemplate;
    }
    
    @Override
    public int save(T t) {
        return t.save();
    }
    
    @Override
    public int remove(T t) {
        return t.remove();
    }
    
    @Override
    public int update(T t) {
        return t.update();
    }
    
}
